package com.rfy.framework.util;


import com.rfy.base.constant.GlobalConstants;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间工具
 */
public class DateUtil {

    /**
     * 默认时间格式
     */
    public static final String DEFAULT_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String DEFAULT_FORMAT_DAY = "yyyy-MM-dd";

    public static final int TIME_HOUR_LAST = 23;
    public static final int TIME_MINITE_SECOND_LAST = 59;


    /**
     * 日期转换
     *
     * @param pattern
     * @param date
     * @return
     */
    public static String format(String pattern, Date date) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(date);
    }

    /**
     * 获取
     *
     * @return
     */
    public static String getDate() {
        SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_FORMAT);
        Date now = new Date();
        String date = sdf.format(now);

        return date;
    }

    /**
     * 时间转换为date
     *
     * @param s
     * @return
     */
    public static Date stringToDate(String s) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DEFAULT_FORMAT);
        try {
            Date date = simpleDateFormat.parse(s);
            return date;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将时间转换为时间戳
     *
     * @param s
     * @return
     */
    public static String dateToStamp(String s) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DEFAULT_FORMAT);
        try {
            Date date = simpleDateFormat.parse(s);
            long ts = date.getTime();
            return String.valueOf(ts);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 将时间戳转换为时间
     *
     * @param s
     * @return
     */
    public static String stampToDate(Long s) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DEFAULT_FORMAT);
        Date date = new Date(s);
        return simpleDateFormat.format(date);
    }

    public static String dateToStr(Date date, String format) {
        String str = "";
        try {
            DateFormat df = new SimpleDateFormat(format);
            str = df.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return str;
    }

    public static Date timestampStr2Date(String timestamp) {
        long t = Long.parseLong(timestamp);
        return new Date(t);
    }

    /**
     * 获取当天 23:59:59:999的时间戳
     *
     * @return
     */
    public static long getTodayMaxUnixTime() {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY, TIME_HOUR_LAST);
        c.set(Calendar.MINUTE, TIME_MINITE_SECOND_LAST);
        c.set(Calendar.SECOND, TIME_MINITE_SECOND_LAST);
        return c.getTimeInMillis() / GlobalConstants.UNIT_MILLSECOND2SECOND;
    }

    /**
     * 邦我寄 返回预约时间处理，开始时间：今天 18:47   结束时间：18：47
     *
     * @param timestamp
     * @param ifBegin
     * @return
     */
    public static String timestamp2BwjTime(String timestamp, boolean ifBegin) {
        if (StringUtil.isBlank(timestamp)) {
            return timestamp;
        }
        Date now = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        // 今天几号
        int day = calendar.get(Calendar.DAY_OF_YEAR);

        calendar.setTime(new Date(Long.parseLong(timestamp)));
        // 预约时间几号
        int d = calendar.get(Calendar.DAY_OF_YEAR);
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        String timeStr;
        try {
            timeStr = sdf.format(new Date(Long.parseLong(timestamp)));
        } catch (Exception e) {
            return timestamp;
        }
        String prefix = "";
        if (ifBegin) {
            if (day == d) {
                // 今天
                prefix = "今天";
            } else if (d - day == 1) {
                // 明天
                prefix = "明天";
            } else {
                // 后天
                throw new RuntimeException("预约时间有误!");
            }
        }
        return prefix + timeStr;
    }

    /**
     * 计算邦我寄开始时间
     *
     * @param beginOrEndTime 格式"08:15"
     * @param date           一般传当前日期时间(new Date())
     * @return
     */
    public static long getBwjAcceptTime(String beginOrEndTime, Date date, int decrease) {
        String[] split = beginOrEndTime.split(":");
        String h = split[0];
        String m = split[1];
        int hour = Integer.parseInt(h);
        int minute = Integer.parseInt(m);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, minute);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.add(Calendar.MINUTE, decrease);
        return calendar.getTime().getTime();
    }

    /**
     * 验证码是否超时
     *
     * @param createTime 时间
     * @param overMinute 超过时间
     * @return
     */
    public static boolean isTimeOver(Date createTime, int overMinute) {
        long l = System.currentTimeMillis() - createTime.getTime();
        Double min = l / 1000.0 / 60.0;
        if (min > overMinute) {
            return true;
        }
        return false;
    }

    /**
     * 验证码是否超时（10分钟）
     *
     * @param createTime 时间
     * @return
     */
    public static boolean isTimeOver(Date createTime) {
        return isTimeOver(createTime, 10);
    }

    /**
     * 比较当前时间是否在某段时间内
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static boolean isBewteen(Date startDate, Date endDate) {
        return isBetween(new Date(), startDate, endDate);
    }

    /**
     * 比较时间是否在某段时间内
     *
     * @param date
     * @param startDate
     * @param endDate
     * @return
     */
    public static boolean isBetween(Date date, Date startDate, Date endDate) {
        return date.before(endDate) && date.after(startDate);
    }

    /**
     * 减去的分钟
     *
     * @param time   格式为19:00
     * @param minute 需要减去的分钟
     * @return
     */
    public static String subtractTimeHour(String time, int minute) {
        String timeStr = getCurrentDate() + " " + time;
        return subtractTime(timeStr, minute);
    }

    /**
     * 减去的分钟
     *
     * @param time   2019-05-21 19:00
     * @param minute 需要减去的分钟
     * @return
     */
    public static String subtractTime(String time, int minute) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        try {
            Date date = sdf.parse(time);
            long timeDate = date.getTime() - minute * 1000 * 60;
            String timeDateStr = sdf.format(timeDate);
            return timeDateStr.split(" ")[1];
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String getCurrentDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dateNow = sdf.format(new Date());
        return dateNow;
    }

    /**
     * 返回今天后的num天
     *
     * @param today
     * @return
     */
    public static String getTimeDate(Date today, int num) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(today);
        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + num);
        Date time = calendar.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(time);
    }

    /**
     * 结束时间与现在时间对比是否小于等于15分钟
     */
    public static boolean isDeliveryNow(String endTime) {
        String endDateStr = getCurrentDate() + " " + endTime;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date endDate = null;
        try {
            endDate = sdf.parse(endDateStr);

            Date nowDate = new Date();
            long result = (endDate.getTime() - nowDate.getTime()) / (1000 * 60);
            if (result <= 15) {
                return false;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return true;
    }

    public static Date convertStrToDate(String dateStr) {
        Date date = null;
        try {
            // 小写的mm表示的是分钟
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            date = sdf.parse(dateStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 当前时间增加2小时
     * 获得预约结束时间
     *
     * @param beginAcceptTime
     * @return
     */
    public static String addTwoHours(String beginAcceptTime) {
        Date current = DateUtil.timestampStr2Date(beginAcceptTime);

        Calendar ca = Calendar.getInstance();
        ca.setTime(current);

        /**
         * 增加2小时
         * */
        ca.add(Calendar.HOUR_OF_DAY, 2);

        return String.valueOf(ca.getTime().getTime());
    }


    /**
     * 时间戳转换成日期格式字符串
     *
     * @param seconds 精确到秒的字符串
     * @param
     * @return
     */
    public static String timeStamp2Date(long seconds) {
        String format = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(Long.valueOf(String.valueOf(seconds) + "000")));
    }
}
